import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';

import 'package:get/get.dart';
import 'package:grouped_list/grouped_list.dart';
import 'package:smart_translator/app/utils/CommonUtil.dart';
import 'package:smart_translator/app/widgets/CommonWidgets.dart';

import '../../../db/database.dart';
import '../../../style/app_colors.dart';
import '../../../style/widget_style.dart';
import '../../../widgets/AppHeader.dart';
import '../controllers/history_records_controller.dart';

class HistoryRecordsView extends GetView<HistoryRecordsController> {
  const HistoryRecordsView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Stack(
          children: [
            Container(
              child: SvgPicture.asset(
                "assets/images/header_part_bg.svg",
                width: Get.width,
                fit: BoxFit.fitWidth,
              ),
            ),
            AppHeader.obtainHeaderWithoutRightPart('历史记录'),
            Obx(()=>Container(
              width: Get.width,
              margin: EdgeInsets.only(top: 85.h),
              padding: EdgeInsets.all(20.w),
              child: Column(
                children: [
                  Container(
                    height: 40.h,
                    padding: EdgeInsets.only(left: 10.w),
                    decoration: BoxDecoration(
                      color: AppColors.swith_inactive_bg_color,
                      borderRadius: BorderRadius.circular(8), // Optional: add border radius
                    ),
                    child: TextField(
                      controller: controller.searchEditeController,
                      decoration: InputDecoration(
                        border: InputBorder.none,
                        focusedBorder: InputBorder.none,
                        contentPadding:EdgeInsets.only(left: 10.w),
                        prefixIcon: SizedBox(
                          child: Container(
                            child: Image.asset('assets/images/icon_searchOrReplace_searching.png'),
                          ),
                          width: 16.w,
                          height: 16.h,
                        ),
                        prefixIconConstraints: BoxConstraints(
                          minWidth: 16.w, // 最小宽度
                          minHeight: 16.h, // 最小高度
                        ),
                        hintText: '输入搜索内容',
                        hintStyle: TextStyle(
                            fontSize: 14.sp
                        ),

                      ),
                      onChanged: (v) {
                          controller.searchContent();
                      },
                    ),
                  ),
                  Expanded(
                      child: controller.showSearchResultList.value ? (controller.searchEmptyData.value? Container(
                        child: Column(
                          children: [
                            SizedBox(height: 80.h,),
                            Image.asset('assets/images/empty_list_realtime.png',width: 169.w,height: 91.h),
                            SizedBox(height: 16.h,),
                            Text('暂无数据',style: AppStyleThemeData.HINT_TEXT_STYLE_small,)
                          ],
                        ),
                      ) : ListView.builder(
                          itemBuilder: (context, index) {
                            TranslationHistoryData data =
                            controller.searchDataList.value[index];
                            return Column(
                              children: [
                                Container(
                                  width: Get.width,
                                  padding: EdgeInsets.all(10.w),
                                  decoration: BoxDecoration(
                                      borderRadius: BorderRadius.all(Radius.circular(10)),
                                      color: Colors.white
                                  ),
                                  child: Column(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                      Text(data.raw_text,style: TextStyle(
                                        fontSize: 14.sp,
                                      ),),
                                      Text(data.translation,style: TextStyle(
                                          fontSize: 14.sp,
                                          color: AppColors.second_title_color
                                      ))
                                    ],
                                  ),
                                ),
                                CommonWidgets.getMyDivider()
                              ],
                            );
                          },
                          itemCount: controller.searchDataList.value.length
                      )) :
                      (controller.emptyData.value ? Container(
                        child: Column(
                          children: [
                            SizedBox(height: 80.h,),
                            Image.asset('assets/images/empty_list_realtime.png',width: 169.w,height: 91.h),
                            SizedBox(height: 16.h,),
                            Text('暂无数据',style: AppStyleThemeData.HINT_TEXT_STYLE_small,)
                          ],
                        ),
                      ) : GroupedListView(
                          elements: controller.translateHistory.value,
                          groupBy: (TranslationHistoryData element) {
                            return CommonUtil.getDateTimeByTakId(element.create_time);
                          },
                          groupSeparatorBuilder: (String groupByValue) => Text(groupByValue),
                          itemBuilder: (context, TranslationHistoryData element) => Column(
                            children: [
                              Container(
                                width: Get.width,
                                padding: EdgeInsets.all(10.w),
                                decoration: BoxDecoration(
                                    borderRadius: BorderRadius.all(Radius.circular(10)),
                                    color: Colors.white
                                ),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(element.raw_text,style: TextStyle(
                                      fontSize: 14.sp,
                                    ),),
                                    Text(element.translation,style: TextStyle(
                                        fontSize: 14.sp,
                                        color: AppColors.second_title_color
                                    ))
                                  ],
                                ),
                              ),
                              CommonWidgets.getMyDivider()
                            ],
                          ),
                          itemComparator: (item1, item2) {
                             return item1.create_time.millisecondsSinceEpoch - item2.create_time.millisecondsSinceEpoch;
                          },
                          groupComparator:(item1,item2) {
                            if(CommonUtil.parseChineseDate(item1).isBefore(CommonUtil.parseChineseDate(item2))) {
                              return 1;
                            } else {
                              return -1;
                            }
                          }
                      ))
                  )


                ],
              ),
            ))
          ],
        )
    );
  }
  
}
